package library.and.xps.com.step.view.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.utils.JUtils;

import java.util.ArrayList;

import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.view.customview.WheelView;


public class SettingsFragment extends Fragment implements View.OnClickListener {
    WheelView wheelViewPlan;
    WheelView wheelViewHeight;
    ArrayList<String> plans = new ArrayList<>();
    ArrayList<String> height = new ArrayList<>();
    TextView tv_dailyplan;
    TextView tv_dailyplan_change, tv_user_height, tv_modify_height;
    int storedPlanIndex;
    int storedHeightIndex;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        tv_user_height = (TextView) view.findViewById(R.id.tv_user_height);
        tv_modify_height = (TextView) view.findViewById(R.id.tv_modify_height);
        tv_modify_height.setOnClickListener(this);
        tv_dailyplan = (TextView) view.findViewById(R.id.tv_daily_plan);
        tv_dailyplan_change = (TextView) view.findViewById(R.id.tv_dailyplan_change);
        tv_dailyplan_change.setOnClickListener(this);

        for(int i = 2000;i<=20000;i+=1000){
            plans.add(i+"");
        }

        for(int i = 130;i<200;i+=2){
            height.add(i+"cm");
        }

        //已选择的计划 所在的index
        String plan = JUtils.getSharedPreference().getString(SPConstant.USER_STEP_PLAN, "9000");
        storedPlanIndex = plans.indexOf(plan);
        tv_dailyplan.setText(plan);


        String heightStr = JUtils.getSharedPreference().getString(SPConstant.USER_HEIGHT, "170cm");
        storedHeightIndex = height.indexOf(heightStr);
        tv_user_height.setText(heightStr);

        return view;
    }

    void showPlanDialog(int selectedIndex) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_daily_plan, null);

        AlertDialog.Builder ab = new AlertDialog.Builder(getActivity()).setView(v).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                JUtils.getSharedPreference().edit().putString(SPConstant.USER_STEP_PLAN, wheelViewPlan.getSeletedItem()).commit();
                tv_dailyplan.setText(wheelViewPlan.getSeletedItem());
                storedPlanIndex = plans.indexOf(wheelViewPlan.getSeletedItem());

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });

        wheelViewPlan = (WheelView) v.findViewById(R.id.wv_view);
        wheelViewPlan.setItems(plans);
        wheelViewPlan.setSeletion(selectedIndex);

        AlertDialog alertDialog = ab.create();
        alertDialog.show();
    }

    void showHeightDialog(int selectedIndex) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_daily_plan, null);

        AlertDialog.Builder ab = new AlertDialog.Builder(getActivity()).setView(v).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                JUtils.getSharedPreference().edit().putString(SPConstant.USER_HEIGHT, wheelViewHeight.getSeletedItem()).commit();
                tv_user_height.setText(wheelViewHeight.getSeletedItem());
                storedHeightIndex = height.indexOf(wheelViewHeight.getSeletedItem());

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        wheelViewHeight = (WheelView) v.findViewById(R.id.wv_view);
        wheelViewHeight.setItems(height);
        wheelViewHeight.setSeletion(selectedIndex);

        AlertDialog alertDialog = ab.create();
        alertDialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_modify_height:
                showHeightDialog(storedHeightIndex);
                break;
            case R.id.tv_dailyplan_change:
                showPlanDialog(storedPlanIndex);
                break;
        }
    }
}
